package com.bone.spider;

import javax.inject.Inject;

import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import com.bone.spider.crawler.DoubanGroupCrawler;
import com.bone.spider.repository.DoubanGroupRepository;

/**
 * @author learnting
 * @date 2020-03-16
 */
@Component
public class DoubanApplicationRunner implements ApplicationRunner, ApplicationContextAware {

  @Inject
  private DoubanGroupRepository doubanGroupRepository;

  private ConfigurableApplicationContext applicationContext;
  @Override
  public void run(ApplicationArguments args) throws Exception {
    DoubanGroupCrawler crawler = new DoubanGroupCrawler("crawl", true, doubanGroupRepository);
    /*start crawl with depth of 4*/
    crawler.start(Integer.MAX_VALUE);
    
    
    applicationContext.close();
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = (ConfigurableApplicationContext) applicationContext;
  }
}
